package com.ruoyi.web.controller.front;

import com.ruoyi.api.domain.UserEntity;

import com.ruoyi.api.repository.UserRepository;
import com.ruoyi.api.service.UserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;

import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;

import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/system/front/user")
public class UserController  extends BaseController {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;
    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserEntity user,
                              @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize)
    {
        startPage();
        // 调用服务层方法，获取分页数据
        Page<UserEntity> resultPage = userService.selectUserList(user,pageNum-1,pageSize);
        // 调用服务层方法，获取分页数据

        // 转换分页结果为 TableDataInfo
        return getDataTable(resultPage.getContent(), resultPage.getTotalElements());
    }


    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserEntity user)
    {

        user.setCreateBy(getUsername());
        UserEntity userEntity = userRepository.findByEmail(user.getEmail());
        if(null!=userEntity){
            throw new ServiceException("该邮箱已被注册");
        }
        user.setPassWord(SecurityUtils.encryptPassword(user.getPassWord()));
//        user.setPassWord(Argon2Utils.hashPassword(user.getPassWord()));
        userRepository.save(user);
        return AjaxResult.success();
    }
    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "修改", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult update(@RequestBody UserEntity user)
    {
//        user.setPassWord(Argon2Utils.hashPassword(user.getPassWord()));
        user.setPassWord(SecurityUtils.encryptPassword(user.getPassWord()));
        return AjaxResult.success(userService.update(UserEntity.class,user,mongoTemplate));
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) String id)
    {
        return AjaxResult.success(userService.getInfoById(UserEntity.class,id,mongoTemplate));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return AjaxResult.success(userService.deleteByIds(UserEntity.class,ids,mongoTemplate));
    }

}
